package com.panda.framework.netty.handler.extend;


import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumErrorCode;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.TransportMessage;
import com.alibaba.fastjson.JSON;
import com.panda.common.core.domain.AjaxResult;
import com.panda.common.core.domain.entity.SysUser;
import com.panda.framework.netty.common.Constant;
import com.panda.framework.netty.utils.MessageUtil;
import com.panda.system.domain.PandaTaskDetail;
import com.panda.system.service.IPandaTaskDetailService;
import com.panda.system.service.ISysUserService;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class SaveTaskDatilWebsocketHandler {
    Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    IPandaTaskDetailService service;

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 保存任务明细
     * @author wechatno:tangjinjinwx
     * @param ctx
     * @param vo
     */
    @Async
    public  void handleMsg(ChannelHandlerContext ctx , TransportMessage vo, String contentJsonStr) {
        try {
            log.debug(contentJsonStr);
            PandaTaskDetail req =  JSON.parseObject(contentJsonStr,PandaTaskDetail.class);
            if(null != req){
                SysUser user = iSysUserService.selectUserById(req.getResponsibleUserId());
                if(null != user){
                    sendJson(ctx, service,req,user);
                }else{
                    MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_PARAMERROR);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
        }
    }

    private static void  sendJson(ChannelHandlerContext ctx, IPandaTaskDetailService service,PandaTaskDetail dto,SysUser user){
        AjaxResult ajaxResult = service.saveTaskDetail(dto, user);
        String json = null ;
        json = JSON.toJSONString(ajaxResult);
        //3、告诉PC客户端消息已收到
        MessageUtil.sendCustomJsonMsg(ctx, "SaveTaskDatilResp", json);
    }


}
